package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Guanxing
 * @date 2021/9/25
 */
@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    @Override
    public RecommendUser findBestUser(Long loginUserId) {
        Query query = new Query(
                Criteria.where("toUserId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);

        //查询数据
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //构建假数据
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(6L);
            recommendUser.setScore(99D);
        }

        //返回数据
        return recommendUser;
    }

    //推荐列表
    @Override
    public PageBeanVo findRecommendUserList(Integer pageNum, Integer pageSize, Long loginUserId) {
        int startIndex = (pageNum - 1) * pageSize;
        if (pageNum == 1) {
            startIndex = 1;
        }

        //设置条件
        Query query = new Query(
                Criteria.where("toUserId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("score")))
                .skip(startIndex).limit(pageSize);

        //查询数据
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);

        //判断是否有假数据
        if(CollectionUtil.isEmpty(recommendUserList)){
            recommendUserList = new ArrayList<>();
            for(int i = 2 ; i < 10 ; i++){
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId( Long.valueOf( i )   ); //设置id
                recommendUser.setScore(RandomUtil.randomDouble(66,99));//随机分数
                recommendUserList.add(recommendUser);
            }
        }

        return new PageBeanVo(pageNum,pageSize,count,recommendUserList);
    }

    //查询缘分值
    @Override
    public RecommendUser findFateValue(Long loginUserId, Long recommendUserId) {
        //设置条件
        Query query = new Query(
                Criteria.where("userId").is(recommendUserId)
                        .and("toUserId").is(loginUserId)
        );

        //查询所需对象
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setScore(RandomUtil.randomDouble(66,99));// 跟之前缘分值不一定一样
        }

        return recommendUser;
    }
}
